import {specsList,specsCount} from '../request/api'
const state = {
    specslist:[],
    pageSize: 2,
    pageNum: 1,
    count:0
}
const getters = {
    getSpecsList(state) {
        console.log(state.specslist);
        return state.specslist
    },
    getSize(state) {
        return state.pageSize
    },
    getCount(state) {
        return state.count
    }
}
const mutations = {
    REQ_SPECSLIST(state, payload) {
        state.specslist=payload
    },
    REQ_COUNT(state, payload) {
        state.count = payload
    },
    REQ_PAGENUM(state, payload) {
        state.pageNum = payload
    }
}
const actions = {
    getSpecsAction(context) {
        specsList({
            size: context.state.pageSize,
            page: context.state.pageNum
        }).then(res => {
            console.log(res);
            let list = res.list ? res.list : []
            if (context.state.pageNum != 1 && list.length == 0) {
                context.dispatch('changePageAction',context.state.pageNum-1)
            }
            context.commit('REQ_SPECSLIST',res.list)
        })
    },
    getCountAction({ commit}) {
        specsCount().then(res => {
            console.log(res);
            commit('REQ_COUNT',res.list[0].total)
        })
    },
    //封装切换页码并重新触发列表的事件
    changePageAction(context, payload) {
        //修改页码
        context.commit('REQ_PAGENUM', payload)
        //重新触发行动(接口列表)
        context.dispatch('getSpecsAction')
    }
}

export default {
    state,
    getters,
    mutations,
    actions,
    namespaced:true
}